﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 党明
  //文件功能描述：前台用户登录处理程序
  //创建人：党明
  //生成时间：2012/04/24 22:15:00
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shopping.Common;
using Shopping.Model;
using Shopping.BLL;

public partial class AS_LoginCommon : BasePage
{
    private readonly UsersManage bllUser = new UsersManage();
    private ReturnValue ret = new ReturnValue();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string action = Request.QueryString["Action"] as string;
            switch (action)
            {
                case "Login":   //
                    Login();
                    break;
                case "Register":
                    Register();
                    break;

            }
            Response.End();
        }
    }

    #region "会员登录  by 赵国伟 2012-08-17"
    /// <summary>
    /// 登录。
    /// </summary>
    private void Login()
    {
        string userName = Utility.P("LoginName");
        string pwd = Utility.P("LoginPwd");
        string code = Utility.P("VerifyCode");
        bool isAutologin = Utility.Cast2bool(Utility.P("IsAutoLogin"));
        bool isRemberUserName = Utility.Cast2bool(Utility.P("IsRemberUserName"));
        
        ReturnValue ret = new ReturnValue(ResultType.Fail, "用户名密码错误！");
        if (code.ToLower() != Utility.GetCookies("CheckCode").ToLower())
        {
            ret.Message = "验证码输入错误！";
            Response.Write(ret.ToJsonString());
            Response.End();
        }
        if (userName.Trim() == "")
        {
            ret.Message = "请输入用户名！";
            Response.Write(ret.ToJsonString());
            Response.End();
        }
        if (pwd == "")
        {
            ret.Message = "请输入登录密码！";
            Response.Write(ret.ToJsonString());
            Response.End();
        }

        if (bllUser.IsCanLogin(userName, Utility.MD5(pwd)).IsSucc)
        {
            Users u = bllUser.GetUserInfoByName(userName);
            ret.Result = ResultType.Success;
            ret.Message = "登录成功！";

            bllUser.Login(u);

            Utility.SetCookies("IsRemberUserName", isRemberUserName.ToString(), "", 7 * 24);
            Utility.SetCookies("IsAutologin", isAutologin.ToString(), "", 7 * 24);
            if (isAutologin || isRemberUserName)
                Utility.SetCookies("tmpUserName", userName, "", 7 * 24);
            if (isAutologin)
                Utility.SetCookies("tmpUserPwd", pwd, "", 7 * 24);
        }
        Response.Write(ret.ToJsonString());
    }
    #endregion

    #region "注册新会员 by 赵国伟 2012-08-17"
    /// <summary>
    /// 注册新会员
    /// </summary>
    private void Register()
    {
        string userName = Utility.P("UserName");
        string passWord = Utility.P("PassWord");
        string email = Utility.P("Email");

        Users u = new Users();
        u.UserName = userName;
        u.PassWord = Utility.MD5(passWord);
        u.Email = email;
        u.RegisterDate = DateTime.Now;
        u.Status = UserStatus.正用.ToString();
        u.LastLoginDate = DateTime.Now;
        u.LastLoginIP = Utility.GetClientIPAddress();
        UsersManage bllUser = new UsersManage();
        Response.Write(bllUser.Register(u).ToJsonString());
    }
    #endregion


}
